package com.whately.core.base.swagger;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zjx
 * @date 2020/4/22
 **/
@Slf4j
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
@Profile({"dev", "local"}) //只本地和测试环境开启
@ConditionalOnProperty(prefix = "swagger",name="enable",havingValue = "true",matchIfMissing = true)
public class SwaggerConfig {
    @Bean
    public Docket api() {
        //=====添加head参数start============================

        List<Parameter> pars = new ArrayList<Parameter>();

        // 方式二
       /* Parameter builder = new ParameterBuilder()
                .name("debug")
                //.parameterType("parameterType")
                .defaultValue("")
                .build();*/
        ParameterBuilder paramPar3 = new ParameterBuilder();
        paramPar3.name("debug").description("debug 开关").modelRef(new ModelRef("string")).parameterType("query").required(false).build();
        pars.add(paramPar3.build());

        ParameterBuilder paramPar2 = new ParameterBuilder();
        paramPar2.name("debugId").description("debug 用户id").modelRef(new ModelRef("long")).parameterType("query").required(false).build();
        pars.add(paramPar2.build());

        ParameterBuilder paramPar1 = new ParameterBuilder();
        paramPar1.name("debugRole").description("debug 用户角色").modelRef(new ModelRef("int")).parameterType("query").required(false).build();
        pars.add(paramPar1.build());

        /**
        ParameterBuilder paramPar = new ParameterBuilder();
        paramPar.name("head").description("head").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(paramPar.build());
        */

        // =========添加head参数end===================
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 自行修改为自己的包路径
                //.apis(RequestHandlerSelectors.basePackage("com.whately.gw.labour"))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
        return docket;
    }

    @Bean
    public UiConfiguration uiConfig() {
        return UiConfigurationBuilder.builder()
                .deepLinking(true)
                .displayOperationId(false)
                // 隐藏UI上的Models模块
                .defaultModelsExpandDepth(-1)
                .defaultModelExpandDepth(0)
                .defaultModelRendering(ModelRendering.EXAMPLE)
                .displayRequestDuration(false)
                .docExpansion(DocExpansion.NONE)
                .filter(false)
                .maxDisplayedTags(null)
                .operationsSorter(OperationsSorter.ALPHA)
                .showExtensions(false)
                .tagsSorter(TagsSorter.ALPHA)
                .validatorUrl(null)
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("api文档")
                .description("接口")
                .version("1.0")
                .build();
    }

    /**
     * 防止@EnableMvc把默认的静态资源路径覆盖了，手动设置的方式
     *
     * @param registry
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决swagger无法访问
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

    }
     */

}
